package com.ebelter.btcomlib.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;


public class MyChartView extends View {

    public static final String TAG  = "MyChartView";

    public MyChartView(Context context) {
        this(context,null);
    }

    public MyChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    public MyChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        init(context, attrs, defStyleAttr);



    }


    private int color1  = 0xffffffff;
    private int color2= 0xffffffff;
    private int color3= 0xffffffff;
    private int color4= 0xffffffff;
    private int xAxleColor= 0xffffffff;
    private int xTextColor= 0xffffffff;
    private int xInterval= 0xffffffff;
    private float yStartValue= 0;
    private float yEndValue= 100;


    private int mWidth;
    private int mHeight;
    private Paint mPaint;

    //初始化
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyChartView, defStyleAttr, 0);
        int count = array.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = array.getIndex(i);
            if (attr==R.styleable.MyChartView_color1){
                color1 = array.getColor(attr, color1);
            }else if (attr==R.styleable.MyChartView_color2){//xy坐标轴宽度
                color2 =array.getColor(attr, color2);
            }else if (attr==R.styleable.MyChartView_color3){//xy坐标轴文字颜色
                color3 = array.getColor(attr, color3);
            }else if (attr==R.styleable.MyChartView_color4){//xy坐标轴文字大小
                color4 = array.getColor(attr, color4);
            }else if (attr==R.styleable.MyChartView_xAxleColor){//折线图中折线的颜色
                xAxleColor = array.getColor(attr, xAxleColor);
            }else if (attr==R.styleable.MyChartView_xTextColor){//折线图中折线的2颜色
                xTextColor = array.getColor(attr, xTextColor);
            }else if (attr==R.styleable.MyChartView_xInterval){//x轴各个坐标点水平间距
                xInterval = (int) array.getDimension(attr,50);
            }else if (attr==R.styleable.MyChartView_yStartValue){//背景颜色
                yStartValue = array.getFloat(attr, 0);
            }else if (attr==R.styleable.MyChartView_yEndValue){//是否在ACTION_UP时，根据速度进行自滑动
                yEndValue = array.getFloat(attr, 100);
            }
        }
        array.recycle();

        LogUtils.i(TAG,
                "----------color1 = "+ NumUtils.toHexString(color1)+
                "\n-----------color2 = "+ NumUtils.toHexString(color2)+
                "\n-----------color3 = "+ NumUtils.toHexString(color3)+
                "\n-----------color4 = "+ NumUtils.toHexString(color4)+
                "\n-----------xAxleColor = "+ NumUtils.toHexString(xAxleColor)+
                "\n-----------xTextColor = "+ NumUtils.toHexString(xTextColor)+
                "\n-----------xInterval = "+ xInterval+
                "\n-----------yStartValue = "+ yStartValue+
                "\n-----------yEndValue = "+ yEndValue
        );


        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }


    /**
     * dp转化成为px
     */
    private int dpToPx(int dp) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f * (dp >= 0 ? 1 : -1));
    }

    /**
     * sp转化为px
     */
    private int spToPx(int sp) {
        float scaledDensity = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (scaledDensity * sp + 0.5f * (sp >= 0 ? 1 : -1));
    }
}
